package com.restaurant.application.login;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.restaurant.application.genshanguser.EncryptSHA;
import com.restaurant.application.genshanguser.GenShangUser;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListBean;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListCriteria;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListImpl;
import com.restaurant.application.login.support.AbstractLogin;
import com.restaurant.application.login.support.LoginData;
import com.restaurant.application.login.support.LoginPageData;

import java.util.List;
import java.util.Objects;

public class LoginImpl extends AbstractLogin {

    private static final String MSG_KEY = "msg::key";
    private static final String ADMIN = "admin";

    @Override
    public ViewUpdate login(LoginData param, int... seq) {

        String userName = param.getUserName();
        String password = param.getPassword();

        if (Strings.isNullOrEmpty(userName) || Strings.isNullOrEmpty(password)) {
            addToRequestCache(MSG_KEY, "用户名和密码不可为空");
            return returnView(getFullView());
        }

        GenShangUserListCriteria criteria = new GenShangUserListCriteria().withNickNameEquals(userName);
        List<GenShangUserListBean> beans = getIndex(GenShangUserListImpl.class).search(criteria, null, 1);
        if (beans.size() == 0 && ADMIN.equals(userName)) {
            GenShangUser user = createNode(GenShangUser.class);
            user.asFirstUser();
            quickIndexUpdate();
            beans = getIndex(GenShangUserListImpl.class).search(criteria, null, 1);
        }

        if (beans.size() > 0) {
            GenShangUserListBean bean = beans.get(0);
            if (GenShangUser.STATE_FROZEN.equals(bean.getState())) {
                addToRequestCache(MSG_KEY, "用户已冻结");
                return returnView(getFullView());
            }
            String beanPassword = bean.getPassword();
            if (!Strings.isNullOrEmpty(beanPassword) && beanPassword.equals(EncryptSHA.sha256(password))) {
                GenShangUser user = getUnit(GenShangUser.class, bean.getUserUid());
                if (Objects.isNull(getGate())) {
                    setGate(user.getId());
                }
                getLogger().debug("Getui bindAlias {} {}", user.findData().getNickName(), userName);
                return returnUpperUnit(getUpper().getId());
            }
        }
        addToRequestCache(MSG_KEY, "用户名或密码错误");
        return returnView(getFullView());
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    @Override
    public View getFullView() {
        LoginPageData data = new LoginPageData()
                .withLoginPath(getPath(CMD_LOGIN))
                .withErrorMessage(getFromRequestCache(MSG_KEY));
        return newLogin(unitPosition(), data).withDockIn("PageRoot");
    }
}
